$(function(){
    var editor = new baidu.editor.ui.Editor({
        textarea: 'content'
    }),editor_en = new baidu.editor.ui.Editor({
        textarea: 'content_en'
    });
    
    editor.render('content');
    editor_en.render('content_en');
    Z.tab($('ul.tab-button.form a'),$('div.tab-content.form-row'),['active','']);
    $('form').hide();

    var uploadifySetData = function(){
        $('#photo-uploader').uploadifySettings('scriptData', {
            'album':$('input[name=album]').val(),
            'thumb_width': $('input[name=thumb_width]').val(),
            'thumb_height': $('input[name=thumb_height]').val(),
            'user':USER_ID,
            'albumName': 'temp_name_'+TYPE,
            'albumDescription': TYPE
        });
    };

    $('#photo-uploader').uploadify({
        'uploader'  : Z.url('/assets/js/uploadify/uploadify.swf'),
        'script'    :  Z.url('/assets/js/uploadify/photo_upload.php'),
        'cancelImg' : Z.url('/assets/js/uploadify/cancel.png'),
        'folder'    : '/assets/uploads',
        'fileDataName': 'photo_upload',
        'fileExt'     : '*.jpg;*.gif;*.png;*.bmp;*.jpeg;*.JPG;*.GIF;*.PNG;*.BMP;*.JPEG',
        'sizeLimit'   : 2 * 1024 * 1024,
        'queueSizeLimit': 10,
        'multi' :  true,
        'auto'  : false,
        'onSelectOnce' : function(){
            uploadifySetData();
        },
        'onComplete' : function(event,id,fileObject,response,data){
            Z.log(response);
            var responseJson;
            eval('responseJson = '+response+';');
            $('form.entry-form input[name=album]').val(responseJson.data.album);
            uploadifySetData();
        },
        onError: function(event,id,fileObject,errorObject){
            Z.log(event,id,fileObject,errorObject);
        }
    });
    
    $('#photo-uploader-button').click(function(){
        $('#photo-uploader').uploadifyUpload();
        return false;
    });
	
    $('form').submit(function(){
	editor.sync();
	editor_en.sync();
        Z.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            success: function(){
                window.location.reload();
            }
        });
        return false;
    });
    
    
    $('a.edit,a.add').click(function(){
        $('form').show();
        $('div.entry-show').hide();
    });
    
    $('a.back').click(function(){
        $('form').hide();
        $('div.entry-show').show();
    });
});